﻿// data service for upload and download
angular.module('app.common').service('dataService', ['$http',
	function ($http) {
		'use strict';
		// download method to call the download web api controller
		var download = function (source, condition) {
			return $http({
				url: '/api/data/download',
				method: 'POST',
				data: JSON.stringify({ objectName: source, condition: condition }),
				headers: { 'Content-Type': 'application/json' }
			}).success(function (data, status, headers, config) {
			}).error(function (data, status, headers, config) {
			});
		},
		// upload method to call the upload web api controller
		upload = function (target, data) {

			var formattedData = [];
			// put the object into an array of field/value pairs
			for (var property in data) {
				formattedData.push({ Key: property, Value: data[property].toString()});
			};
			// send upload
			return $http({
				url: '/api/data/upload',
				method: 'POST',
				data: JSON.stringify({ objectType: target, values: formattedData}),
				headers: { 'Content-Type': 'application/json' }
			}).success(function (data, status, headers, config) {
				// do something
			}).error(function (data, status, headers, config) {
				// do something
			});
		},
		
		svc = {
			download: download,
			upload: upload
		};

		return svc;
	}]);